# -*- coding: utf-8 -*-

from web.controllers.api import route_api
from flask import request, jsonify
from application import app, db
import requests, json
from common.models.member.Member import Member
from common.models.device.CheckList import CheckList
from common.models.device.DeviceRelation import DeviceRelation
from common.libs.Helper import getCurrentDate


@route_api.route("/testman/detail", methods=["GET", "POST"])
def testman_detail():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}
    content = request.get_json()
    usercode = content['usercode']
    check_id = content['homeid']

    member_info = Member.query.filter_by(usercode=usercode).first()

    if not member_info:
        resp['error'] = -1
        resp['msg'] = "无此用户"
        return jsonify(resp)

    member_id = member_info.id

    checklist_info = CheckList.query.filter_by(id=check_id).first()
    device_info_array = DeviceRelation.query.filter_by(check_id=check_id, testman_name=member_info.nickname).all()

    data = dict()
    device_info_list = []

    data['sjdzt'] = checklist_info.administor_status
    data['sjdw'] = checklist_info.company
    data['wtrq'] = checklist_info.commission_date
    data['lxr'] = checklist_info.contact
    data['dh'] = checklist_info.mobile

    for device_item in device_info_array:
        device_info_list_item = dict()
        device_info_list_item['sbmc'] = device_item.device_name
        device_info_list_item['xhgg'] = device_item.model
        device_info_list_item['zzc'] = device_item.manufacture
        device_info_list_item['dj'] = device_item.rank
        device_info_list_item['ccbh'] = device_item.number
        device_info_list_item['yxrq'] = device_item.date
        device_info_list_item['jcjg'] = device_item.resault
        device_info_list_item['csry'] = device_item.testman_name
        device_info_list_item['sbid'] = device_item.device_id
        device_info_list_item['bhgyy'] = device_item.reason_back
        device_info_list.append(device_info_list_item)

    data['deviceInfo'] = device_info_list

    resp['data'] = data
    return jsonify(resp)


@route_api.route("/testman/commit", methods=["GET", "POST"])
def testman_commit():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}
    content = request.get_json()

    check_id = content['homeid']
    usercode = content['usercode']
    deviceinfo_list = content['deviceInfo']

    for deviceinfo_item in deviceinfo_list:
        device_id = deviceinfo_item['sbid']
        resault = deviceinfo_item['jcjg']
        reason_back = deviceinfo_item['bhgyy']

        device_info = DeviceRelation.query.filter_by(check_id=check_id, device_id=device_id).first()
        device_info.resault = resault
        device_info.reason_back = reason_back
        if reason_back.strip() == "":
            device_info.test_status = "已完成"
        else:
            device_info.test_status = "被打回"
        device_info.update_time = getCurrentDate()

        db.session.commit()

    counter = 0
    # 通过check_id找到所有关联的devie,判断所有关联的device状态test_status都为已完成的话，总体单子才是已完成
    device_query_list = DeviceRelation.query.filter_by(check_id=check_id).all()
    for device_query_item in device_query_list:
        if device_query_item.test_status.strip() == "" or device_query_item.test_status == "待测试":
            counter += 1

    if counter == 0:
        checklist_info = CheckList.query.filter_by(id=check_id).first()
        checklist_info.administor_status = "测试已完成，待反馈至客户"
        db.session.commit()
    return jsonify(resp)



